/*
 *
 * Version 1.00
 * 
 * Created on 2007-6-4
 * Encoding UTF-8
 */
package net.comm.shequ.web.action;

import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import net.comm.oper.service.impl.NotAdvServiceImpl;
import net.comm.oper.web.form.NotAdvForm;
import net.comm.shequ.service.impl.JgShenqingServiceImpl;
import net.comm.shequ.web.form.JgShenqingForm;
import net.comm.common.GlbFinal;
import net.comm.common.ReturnMessage;
import net.comm.group.service.impl.GroupsServiceImpl;

import net.comm.user.bean.BaseUsers;
import net.comm.user.service.impl.DetailsServiceImpl;
import net.comm.user.service.impl.UserHomeServiceImpl;
import net.comm.user.service.impl.UserSysinfoServiceImpl;
import net.comm.user.service.impl.UsersServiceImpl;
import net.comm.user.web.form.DetailsForm;
import net.comm.user.web.form.UserHomeForm;
import net.comm.user.web.form.UserSysinfoForm;
import net.comm.user.web.form.UsersForm;
import net.comm.util.HtmlMessageBox;
import net.comm.web.base.AbstractBaseAction;


/**
 * 机构申请认证Action
 * @author hcgong(hcgong@sina.com)
 * @version 1.0
 * @date 2007-6-4
 */
public class JgShenqingAction extends AbstractBaseAction {
	
	private JgShenqingServiceImpl service = null;
	
	/**
	 * struct-config中该ACTION中的每个forward的常量定义
	 */
	final String LIST_FORWARD    = "list";
	final String OPERATE_FORWARD = "operate";
	final String HOME_FORWARD = "home";
	final String LOGIN_FORWARD = "loginFwd";	
	final String FAQ_FORWARD = "faq";
	
	
	/**
	 *构造方法
	 */
	public JgShenqingAction()
	{
		super(false);
		
		/**
		 * 初始化业务组件接口
		 */
		service = new JgShenqingServiceImpl();
	}
	
	/**
	 * 默认动作
	 * 1.action值为空的时候,进入查询页面
	 * 2.每个操作都执行的代码,但是方法必须返回null
	 * @param mapping	 跳转的mapping值	
	 * @param request	 HttpServletRequest对象
	 * @param response	 HttpServletResponse对象
	 */
	public ActionForward startAction(ActionMapping mapping, ActionForm form,
									 HttpServletRequest request , 
									 HttpServletResponse response)
	{
		String action = getReqParam(request , ACTION_KEY);
		if(action == null || action.trim().length() == 0) {
			return mapping.findForward(LIST_FORWARD);
		} else {
			return null;
		}
	}
	
	/**
	 * 查询所有机构申请认证
	 * @param mapping		跳转的mapping值		
	 * @param request		HttpServletRequest对象
	 * @param response		HttpServletResponse对象
	 * @return
	 * @throws Exception
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form,
							  HttpServletRequest request , 
							  HttpServletResponse response) 
		throws Exception
	{
		/**
		 *定义一个查询bean保存查询页面提交的查询条件
		 */
		JgShenqingForm theForm = (JgShenqingForm)form;
			
		// 生成分页对象
	
		theForm.setPage(this.getPageFromReq(request));
		
		ReturnMessage msg  = new ReturnMessage();
		
		/**
		 *调用services中的方法
		 */
		List list = service.list(theForm , msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		
		/**
		 *设置列表的数据
		 */
		setAction(request, "list");
		request.setAttribute("bloglist" , list);
		request.setAttribute("bcommentlist" , list);
		request.setAttribute("theForm", theForm);
		
		/**
		 * 设置分页对象,key,lastkey到request中
		 * 使用分页组件,必须设置这三个值
		 */
		request.setAttribute(PAGE_KEY , theForm.getPage());
		request.setAttribute(KEY , theForm.getKey());
		request.setAttribute(LAST_KEY , theForm.getLastKey());
	
		//取得动态评论
		
	
		/**
		 * 转发到LIST_FORWARD指定的jsp文件
		 */
		return mapping.findForward(LIST_FORWARD);
	}
	/**
	 * 查询所有机构申请认证
	 * @param mapping		跳转的mapping值		
	 * @param request		HttpServletRequest对象
	 * @param response		HttpServletResponse对象
	 * @return
	 * @throws Exception
	 */
	public ActionForward adlist(ActionMapping mapping, ActionForm form,
							  HttpServletRequest request , 
							  HttpServletResponse response) 
		throws Exception
	{
		/**
		 *定义一个查询bean保存查询页面提交的查询条件
		 */
		JgShenqingForm theForm = (JgShenqingForm)form;
			
		// 生成分页对象
	
		theForm.setPage(this.getPageFromReq(request));
		
		ReturnMessage msg  = new ReturnMessage();
		
		/**
		 *调用services中的方法
		 */
		List list = service.adlist(theForm , msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		
		/**
		 *设置列表的数据
		 */
		setAction(request, "adlist");
		request.setAttribute("shenqinglist" , list);
		request.setAttribute("theForm", theForm);
		
		/**
		 * 设置分页对象,key,lastkey到request中
		 * 使用分页组件,必须设置这三个值
		 */
		request.setAttribute(PAGE_KEY , theForm.getPage());
		request.setAttribute(KEY , theForm.getKey());
		request.setAttribute(LAST_KEY , theForm.getLastKey());
	
		//取得动态评论
		
		/**
		 * 转发到LIST_FORWARD指定的jsp文件
		 */
		return mapping.findForward(LIST_FORWARD);
	}	
	
//	/**
//	 * 查询机构申请认证
//	 * @param mapping		跳转的mapping值		
//	 * @param request		HttpServletRequest对象
//	 * @param response		HttpServletResponse对象
//	 * @return
//	 * @throws Exception
//	 */
//	public ActionForward home(ActionMapping mapping, ActionForm form,
//							  HttpServletRequest request , 
//							  HttpServletResponse response) 
//		throws Exception
//	{
//		/**
//		 *定义一个查询bean保存查询页面提交的查询条件
//		 */
//		JgShenqingForm theForm = (JgShenqingForm)form;
//			
//		// 生成分页对象
//		theForm.setPage(this.getPageFromReq(request));
//		
//		ReturnMessage msg  = new ReturnMessage();
//		//取得机构申请认证信息
//		service.get(theForm, msg);
//		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
//		{
//			HtmlMessageBox hmb = new HtmlMessageBox();
//			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
//			hmb.setPrintMessage(msg.getMsg());
//			hmb.setForwardUrl("javascript:history.go(-1)");
//			return showMsg(hmb , request , response , mapping);
//		}		
//		request.setAttribute("theForm", theForm);
//		
//		//取得机构申请认证成员
//		UserHomeServiceImpl userhomeservice = new UserHomeServiceImpl();
//		UserHomeForm userhomeForm = new UserHomeForm();
//		
//		userhomeForm.setPage(this.getPageFromReq(request,32));
//		userhomeForm.setQryVillageID(theForm.getVillageID());
//		
//		List memberlist = userhomeservice.adlist(userhomeForm, msg);
//		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
//		{
//			HtmlMessageBox hmb = new HtmlMessageBox();
//			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
//			hmb.setPrintMessage(msg.getMsg());
//			hmb.setForwardUrl("javascript:history.go(-1)");
//			return showMsg(hmb , request , response , mapping);
//		}		
//		request.setAttribute("memberlist" , memberlist);
//		
//		//取得机构申请认证部落
//		GroupsServiceImpl groupservice = new GroupsServiceImpl();
//		
//		List grouplist = groupservice.listbyFirst(8);
//		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
//		{
//			HtmlMessageBox hmb = new HtmlMessageBox();
//			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
//			hmb.setPrintMessage(msg.getMsg());
//			hmb.setForwardUrl("javascript:history.go(-1)");
//			return showMsg(hmb , request , response , mapping);
//		}		
//		request.setAttribute("grouplist" , grouplist);	
//		
//		//取得自助机构
//		
//		//
//		
//		/**
//		 * 转发到LIST_FORWARD指定的jsp文件
//		 */
//		return mapping.findForward(HOME_FORWARD);
//	}

	/**
	 * 查询机构申请认证
	 * @param mapping		跳转的mapping值		
	 * @param request		HttpServletRequest对象
	 * @param response		HttpServletResponse对象
	 * @return
	 * @throws Exception
	 */
	public ActionForward get(ActionMapping mapping, ActionForm form,
							  HttpServletRequest request , 
							  HttpServletResponse response) 
		throws Exception
	{
		/**
		 *定义一个查询bean保存查询页面提交的查询条件
		 */
		JgShenqingForm theForm = (JgShenqingForm)form;
			
		// 生成分页对象
		theForm.setPage(this.getPageFromReq(request));
		
		ReturnMessage msg  = new ReturnMessage();
		//取得机构申请认证信息
		service.get(theForm, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}		
		request.setAttribute("theForm", theForm);
		

		//
		
		/**
		 * 转发到LIST_FORWARD指定的jsp文件
		 */
		return mapping.findForward(HOME_FORWARD);
	}
	
	/**
	 * 跳转到添加页面
	 */
	public ActionForward addPage(ActionMapping mapping, ActionForm form,
			 				 	 HttpServletRequest request , 
			 				 	 HttpServletResponse response)  
		throws Exception
	{
		JgShenqingForm theForm = (JgShenqingForm)form;
		theForm.setAction(getReqParam(request, "action"));
		
		request.setAttribute("theForm", theForm);
	
		return mapping.findForward(OPERATE_FORWARD);
	}
	
	/**
	 * 查询公告/通知/投诉/建议/留言清单
	 * @param mapping		跳转的mapping值		
	 * @param request		HttpServletRequest对象
	 * @param response		HttpServletResponse对象
	 * @return
	 * @throws Exception
	 */
	public ActionForward faq(ActionMapping mapping, ActionForm form,
							  HttpServletRequest request , 
							  HttpServletResponse response) 
		throws Exception
	{
		
		ReturnMessage msg  = new ReturnMessage();
		//读取首席客服信息
		UsersServiceImpl userservice = new UsersServiceImpl();
		
		UsersForm userform = new UsersForm();
		
		userform.setQryType(GlbFinal.USER_KEFU);
		userform.setPage(this.getPageFromReq(request,1));
		
		List userlist = userservice.adlist(userform, msg);
		if(msg.getErrCode() == 0 && userlist.size() > 0){
			
			
			BaseUsers base = (BaseUsers)userlist.get(0);
			BeanUtils.copyProperties(userform, base);
			
			//取详细信息
			DetailsServiceImpl detailservice = new DetailsServiceImpl();
			DetailsForm detailform = new DetailsForm();
	
			detailform.setUserID(base.getUserID());
			
			detailservice.get(detailform, msg);
			
			BeanUtils.copyProperties(userform, detailform);
			
			//取居住信息
			UserHomeServiceImpl homeservice = new UserHomeServiceImpl();
			UserHomeForm homeForm = new UserHomeForm();

			homeForm.setUserID(base.getUserID());
			homeservice.getbyuserID(homeForm, msg);
			
			BeanUtils.copyProperties(userform, homeForm);

			//取积分信息
			UserSysinfoServiceImpl syservice = new UserSysinfoServiceImpl();
			UserSysinfoForm sysform = new UserSysinfoForm();
			
			sysform.setUserID(base.getUserID());
			
			syservice.get(sysform, msg);
			
			request.setAttribute("sysform" , sysform);
			
		}
		request.setAttribute("userform" , userform);
		
		/**
		 *定义一个查询bean保存查询页面提交的查询条件
		 */
		NotAdvForm theForm = new NotAdvForm();
		setAction(request, "list");
		
		//取得公告  常见问题 
		NotAdvServiceImpl notadvservice = new NotAdvServiceImpl();
		NotAdvForm notadvForm = new NotAdvForm();
		
		StringBuffer sb = new StringBuffer("");
		sb.append(GlbFinal.NTYPE_GONGGAO);
		sb.append(",");
		sb.append(GlbFinal.NTYPE_WENTI);
		
		notadvForm.setPage(this.getPageFromReq(request,15));
		notadvForm.setQryNtype(sb.toString());
		notadvForm.setQryNsort(GlbFinal.NSORT_KEFU);
		notadvForm.setQryNsortID(0);
		
		List notadvlist = notadvservice.listbyGroup(notadvForm, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}		
		request.setAttribute("notadvlist" , notadvlist);		
		
		request.setAttribute("theForm", theForm);

//		//取得 常见问题
//		notadvForm.Clear();
//		notadvForm.setQryNtype(GlbFinal.NTYPE_WENTI);
//		notadvForm.setNsort(GlbFinal.NSORT_KEFU);
//		
//		List wentilist = notadvservice.adlist(notadvForm, msg);
//		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
//		{
//			HtmlMessageBox hmb = new HtmlMessageBox();
//			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
//			hmb.setPrintMessage(msg.getMsg());
//			hmb.setForwardUrl("javascript:history.go(-1)");
//			return showMsg(hmb , request , response , mapping);
//		}		
//		request.setAttribute("wentilist" , wentilist);			
		
		//投诉
		notadvForm.Clear();
		notadvForm.setPage(this.getPageFromReq(request,5));
		notadvForm.setQryNtype(GlbFinal.NTYPE_TOUSU);
		notadvForm.setNsort(GlbFinal.NSORT_KEFU);
		
		List tousulist = notadvservice.adlist(notadvForm, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}		
		request.setAttribute("tousulist" , tousulist);			
		
		//建议 
		notadvForm.Clear();
		notadvForm.setPage(this.getPageFromReq(request,5));
		notadvForm.setQryNtype(GlbFinal.NTYPE_JIANYI);
		notadvForm.setNsort(GlbFinal.NSORT_KEFU);
		
		List jianyilist = notadvservice.adlist(notadvForm, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}		
		request.setAttribute("jianyilist" , jianyilist);			
		
		/**
		 * 转发到LIST_FORWARD指定的jsp文件
		 */
		return mapping.findForward(FAQ_FORWARD);
	}	
		
	/**
	 * 跳转到编辑页面
	 */
	public ActionForward modifyPage(ActionMapping mapping, ActionForm form,
							 	    HttpServletRequest request , 
							 	    HttpServletResponse response)
		throws Exception
	{
	
		return get(mapping , form, request , response);
	}
	
	/**
	 *添加新机构申请认证
	 */
	public ActionForward add(ActionMapping mapping, ActionForm form,
							 HttpServletRequest request , 
							 HttpServletResponse response)
		throws Exception
	{
		JgShenqingForm theForm = (JgShenqingForm)form;

		ReturnMessage msg  = new ReturnMessage();
		
		service.add(theForm, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}else{
			msg.setMsg("申请认证成功，等待管理人员审核");
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_PRINTTOHTML);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
	}
	
	/**
	 * 修改机构申请认证
	 */
	public ActionForward update(ActionMapping mapping, ActionForm form,
							 	HttpServletRequest request , 
							 	HttpServletResponse response)
		throws Exception
	{

		JgShenqingForm theForm = (JgShenqingForm) form;

		ReturnMessage msg  = new ReturnMessage();
		
		service.update(theForm ,msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		
		return list(mapping , form, request , response);
	}

	/**
	 * 修改机构申请认证状态
	 */
	public ActionForward updsts(ActionMapping mapping, ActionForm form,
							 	HttpServletRequest request , 
							 	HttpServletResponse response)
		throws Exception
	{

		JgShenqingForm theForm = (JgShenqingForm) form;

		ReturnMessage msg  = new ReturnMessage();
		
		service.updsts(theForm ,msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		
		return adlist(mapping , form, request , response);
	}

	/**
	 *删除单个机构申请认证
	 */
	public ActionForward del(ActionMapping mapping, ActionForm form,
							 HttpServletRequest request , 
							 HttpServletResponse response)
		throws Exception
	{
		JgShenqingForm theForm = (JgShenqingForm) form;
		
		ReturnMessage msg  = new ReturnMessage();
		String path = request.getSession().getServletContext().getRealPath("/");
		
		service.delete(theForm ,path, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}

		return adlist(mapping , form, request , response);
	}

	/**
	 * 中文英文切换页面
	 */
	public ActionForward tran(ActionMapping mapping, ActionForm form,
							  HttpServletRequest request , 
							  HttpServletResponse response)
	{
		HttpSession session = request.getSession(true);
		String language = getReqParam(request , "language");
		
		if("en".equals(language)) {
			session.setAttribute(Globals.LOCALE_KEY , Locale.ENGLISH);
		} else {
			session.setAttribute(Globals.LOCALE_KEY , Locale.CHINESE);
		}
		
		return mapping.findForward("tran");
	}
	
	
	/**
	 * 
	 */
	public ActionForward dis(ActionMapping mapping, ActionForm form,
							 HttpServletRequest request , 
							 HttpServletResponse response)
		throws Exception
	{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage("msg.getMsg()");
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
	}	

	/**
	 *删除投诉建议
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
							 HttpServletRequest request , 
							 HttpServletResponse response)
		throws Exception
	{
		JgShenqingForm theForm = (JgShenqingForm) form;
		
		ReturnMessage msg  = new ReturnMessage();
		NotAdvServiceImpl naservice = new NotAdvServiceImpl();
		String SeqID = request.getParameter("SeqID");
		theForm.setSeqID(Integer.parseInt(SeqID));
		naservice.delete1(theForm , msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		
		String tabindex = request.getParameter("tabindex");
		request.setAttribute("tabindex", tabindex);

		return faq(mapping , form, request , response);
	}

}
